<?php
//数据库操作类
class DBHelper{

    function __construct()
    {
        die("Do not create this class object, please use static class func");
    }
    public static function CreateConnect(&$msg = null)
    {
        $con = mysql_connect(GetConfInfo("db_host"), GetConfInfo("db_username"), GetConfInfo("db_password"));
        if (!$con)
        {
            $msg = mysql_error($con);
            return null;
        }

        mysql_query("set names utf8"); //设置字符集

        if(!mysql_select_db(GetConfInfo("db_schema"), $con))  //选择数据库
        {
            $msg = mysql_error($con);
            return null;
        }
        return $con;
    }
    //select查询， 返回数组, 错误返回-1
    public static function Query($str_sql, &$msg = null)
    {
        $con = DBHelper::CreateConnect($msg);
        if ($con == null) return -1;

        $result = mysql_query($str_sql, $con);
        if (!$result)
        {
            $msg = mysql_error($con);
            return -1;
        }

        $returnArray = array();
        while($row = mysql_fetch_assoc($result))
        {
            $returnArray[] = $row;
        }
        mysql_free_result($result);
        mysql_close($con);

        return $returnArray;
    }
    //select查询， 返回JSON数据到js
    public static function QueryReturnJSON($str_sql, &$msg = null)
    {
        $rArray = DBHelper::Query($str_sql, $msg);
        if($rArray == -1)
        {
            ReturnJSONErrorMsg($msg);
            return false;
        }
        ReturnJSONFromResultArray($rArray, "", "");
        return true;
    }
    //执行update 等语句， 返回受影响行数,失败返回-1
    public static function Exec($str_sql, &$msg = null)
    {
        $con = DBHelper::CreateConnect($msg);
        if ($con == null) return -1;

        $result = mysql_query($str_sql, $con);
        if (!$result)
        {
            $msg = mysql_error($con);
            return -1;
        }

        $num = mysql_affected_rows();
        mysql_close($con);

        return $num;
    }
    //执行update 等语句, 返回信息给给web
    public static function ExecAndPrintMsg($str_sql)
    {
        $msg = "";
        if(DBHelper::Exec($str_sql, $msg) == -1)
        {
            PrintMsgToHTML("编辑失败",$msg);
            return false;
        }
        else
        {
            PrintMsgToHTML("编辑成功");
            return true;
        }
    }
    //执行update 等语句, 返回信息给js
    public static function ExecReturnJSON($str_sql)
    {
        $msg = "";
        if(DBHelper::Exec($str_sql, $msg) == -1)
        {
            ReturnJSONMsg("编辑失败",$msg, false);
            return false;
        }
        else
        {
            ReturnJSONMsg("编辑成功");
            return true;
        }
    }
    //insert 返回自动增量ID值, 错误返回-1
    public static function InsertReturnID($str_sql, &$msg = null)
    {
        $con = DBHelper::CreateConnect($msg);
        if ($con == null) return -1;

        $result = mysql_query($str_sql, $con);
        if (!$result)
        {
            $msg = mysql_error($con);
            return -1;
        }

        $str_sql = "select last_insert_id() as id";
        $result = mysql_query($str_sql, $con);
        if (!$result)
        {
            $msg = mysql_error($con);
            return -1;
        }

        $r_id = mysql_fetch_assoc($result);

        mysql_free_result($result);
        mysql_close($con);

        return $r_id["id"];
    }
    //查询select  返回行数, 错误返回-1
    public static function QueryRowCount($str_sql, &$msg = null)
    {
        $con = DBHelper::CreateConnect($msg);
        if ($con == null) return -1;

        $result = mysql_query($str_sql, $con);
        if (!$result)
        {
            $msg = mysql_error($con);
            return -1;
        }

        $num = mysql_num_rows($result);
        mysql_free_result($result);
        mysql_close($con);
        return $num;
    }
    //查询select  行数>0 返回true;
    public static function QueryIsExistRow($str_sql, &$msg = null)
    {
        $r_count = DBHelper::QueryRowCount($str_sql, $msg);
        return ($r_count > 0);
    }
}
 
?>
